package com.example.sps.commons;

import lombok.Data;

import java.sql.PseudoColumnUsage;

/**
 * @ProjectName: Sps
 * @Titile: R
 * @Author: nency
 * @Description: TODO
 */
@Data
public class R<T> {
    private Integer code;
    private String message;
    private T data;

    /**
     * 操作成功，不携带数据
     * @param message
     * @return
     */
    public static R<Void> ok(String message){
        R<Void> r = new R<>();
        r.message = message;
        r.code = ServiceCode.OK.getCode();
        return r;
    }

    /**
     * 成功，携带数据
     * @param message
     * @param data
     * @return
     */
    public static <T> R<T> ok(String message,T data){
        R<T> r = new R<>();
        r.code = ServiceCode.OK.getCode();
        r.message = message;
        r.data = data;
        return r;
    }

//    public static R<Void> fail(String message,Integer code){
//        R<Void> r = new R<>();
//        r.code = code;
//        r.message = message;
//        return r;
//    }

    public static R<Void> fail(ServiceException e){
        R<Void> r = new R<>();
        r.message = e.getMessage();
        r.code = e.getServiceCode().getCode();
        return r;
    }
}
